home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JRDGIF.C < prev    next >
C/C++ Source or Header  |  1992-12-02  |  21KB  |  631 lines

  1. /*
  2.  * jrdgif.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in GIF format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume input from
  12.  * an ordinary stdio stream.  They further assume that reading begins
  13.  * at the start of the file; input_init may need work if the
  14.  * user interface has already read some data (e.g., to determine that
  15.  * the file is indeed GIF format).
  16.  *
  17.  * These routines are invoked via the methods get_input_row
  18.  * and input_init/term.
  19.  */
  20.  
  21. /*
  22.  * This code is loosely based on giftoppm from the PBMPLUS distribution
  23.  * of Feb. 1991.  That file contains the following copyright notice:
  24.  * +-------------------------------------------------------------------+
  25.  * | Copyright 1990, David Koblas.                                     |
  26.  * |   Permission to use, copy, modify, and distribute this software   |
  27.  * |   and its documentation for any purpose and without fee is hereby |
  28.  * |   granted, provided that the above copyright notice appear in all |
  29.  * |   copies and that both that copyright notice and this permission  |
  30.  * |   notice appear in supporting documentation.  This software is    |
  31.  * |   provided "as is" without express or implied warranty.           |
  32.  * +-------------------------------------------------------------------+
  33.  *
  34.  * We are also required to state that
  35.  *    "The Graphics Interchange Format(c) is the Copyright property of
  36.  *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
  37.  *    CompuServe Incorporated."
  38.  */
  39.  
  40. #include "jinclude.h"
  41.  
  42. #ifdef GIF_SUPPORTED
  43.  
  44.  
  45. #define    MAXCOLORMAPSIZE    256    /* max # of colors in a GIF colormap */
  46. #define NUMCOLORS    3    /* # of colors */
  47. #define CM_RED        0    /* color component numbers */
  48. #define CM_GREEN    1
  49. #define CM_BLUE        2
  50.  
  51. static JSAMPARRAY colormap;    /* the colormap to use */
  52. /* colormap[i][j] = value of i'th color component for pixel value j */
  53.  
  54. #define    MAX_LZW_BITS    12    /* maximum LZW code size */
  55. #define LZW_TABLE_SIZE    (1<<MAX_LZW_BITS) /* # of possible LZW symbols */
  56.  
  57. /* Macros for extracting header data --- note we assume chars may be signed */
  58.  
  59. #define LM_to_uint(a,b)        ((((b)&0xFF) << 8) | ((a)&0xFF))
  60.  
  61. #define BitSet(byte, bit)    ((byte) & (bit))
  62. #define INTERLACE    0x40    /* mask for bit signifying interlaced image */
  63. #define COLORMAPFLAG    0x80    /* mask for bit signifying colormap presence */
  64.  
  65. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  66.  
  67. /* Static vars for GetCode and LZWReadByte */
  68.  
  69. static char code_buf[256+4];    /* current input data block */
  70. static int last_byte;        /* # of bytes in code_buf */
  71. static int last_bit;        /* # of bits in code_buf */
  72. static int cur_bit;        /* next bit index to read */
  73. static boolean out_of_blocks;    /* TRUE if hit terminator data block */
  74.  
  75. static int input_code_size;    /* codesize given in GIF file */
  76. static int clear_code,end_code; /* values for Clear and End codes */
  77.  
  78. static int code_size;        /* current actual code size */
  79. static int limit_code;        /* 2^code_size */
  80. static int max_code;        /* first unused code value */
  81. static boolean first_time;    /* flags first call to LZWReadByte */
  82.  
  83. /* LZW decompression tables:
  84.  *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  85.  *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  86.  * Note that entries 0..end_code of the above tables are not used,
  87.  * since those symbols represent raw bytes or special codes.
  88.  *
  89.  * The stack represents the not-yet-used expansion of the last LZW symbol.
  90.  * In the worst case, a symbol could expand to as many bytes as there are
  91.  * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
  92.  * (This is conservative since that number includes the raw-byte symbols.)
  93.  *
  94.  * The tables are allocated from FAR heap space since they would use up
  95.  * rather a lot of the near data space in a PC.
  96.  */
  97.  
  98. static UINT16 FAR *symbol_head; /* => table of prefix symbols */
  99. static UINT8  FAR *symbol_tail; /* => table of suffix bytes */
  100. static UINT8  FAR *symbol_stack; /* stack for symbol expansions */
  101. static UINT8  FAR *sp;        /* stack pointer */
  102.  
  103. /* Static state for interlaced image processing */
  104.  
  105. static boolean is_interlaced;    /* TRUE if have interlaced image */
  106. static big_sarray_ptr interlaced_image;    /* full image in interlaced order */
  107. static long cur_row_number;    /* need to know actual row number */
  108. static long pass2_offset;    /* # of pixel rows in pass 1 */
  109. static long pass3_offset;    /* # of pixel rows in passes 1&2 */
  110. static long pass4_offset;    /* # of pixel rows in passes 1,2,3 */
  111.  
  112.  
  113. /* Forward declarations */
  114. METHODDEF void load_interlaced_image PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  115. METHODDEF void get_interlaced_row PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  116.  
  117.  
  118.  
  119. LOCAL int
  120. ReadByte (compress_info_ptr cinfo)
  121. /* Read next byte from GIF file */
  122. {
  123.   register FILE * infile = cinfo->input_file;
  124.   int c;
  125.  
  126.   if ((c = getc(infile)) == EOF)
  127.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  128.   return c;
  129. }
  130.  
  131.  
  132. LOCAL int
  133. GetDataBlock (compress_info_ptr cinfo, char *buf)
  134. /* Read a GIF data block, which has a leading count byte */
  135. /* A zero-length block marks the end of a data block sequence */
  136. {
  137.   int count;
  138.  
  139.   count = ReadByte(cinfo);
  140.   if (count > 0) {
  141.     if (! ReadOK(cinfo->input_file, buf, count))
  142.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  143.   }
  144.   return count;
  145. }
  146.  
  147.  
  148. LOCAL void
  149. SkipDataBlocks (compress_info_ptr cinfo)
  150. /* Skip a series of data blocks, until a block terminator is found */
  151. {
  152.   char buf[256];
  153.  
  154.   while (GetDataBlock(cinfo, buf) > 0)
  155.     /* skip */;
  156. }
  157.  
  158.  
  159. LOCAL void
  160. ReInitLZW (void)
  161. /* (Re)initialize LZW state; shared code for startup and Clear processing */
  162. {
  163.   code_size = input_code_size+1;
  164.   limit_code = clear_code << 1;    /* 2^code_size */
  165.   max_code = clear_code + 2;    /* first unused code value */
  166.   sp = symbol_stack;        /* init stack to empty */
  167. }
  168.  
  169.  
  170. LOCAL void
  171. InitLZWCode (void)
  172. /* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  173. {
  174.   /* GetCode initialization */
  175.   last_byte = 2;        /* make safe to "recopy last two bytes" */
  176.   last_bit = 0;            /* nothing in the buffer */
  177.   cur_bit = 0;            /* force buffer load on first call */
  178.   out_of_blocks = FALSE;
  179.  
  180.   /* LZWReadByte initialization */
  181.   clear_code = 1 << input_code_size; /* compute special code values */
  182.   end_code = clear_code + 1;    /* note that these do not change */
  183.   first_time = TRUE;
  184.   ReInitLZW();
  185. }
  186.  
  187.  
  188. LOCAL int
  189. GetCode (compress_info_ptr cinfo)
  190. /* Fetch the next code_size bits from the GIF data */
  191. /* We assume code_size is less than 16 */
  192. {
  193.   register INT32 accum;
  194.   int offs, ret, count;
  195.  
  196.   if ( (cur_bit+code_size) > last_bit) {
  197.     /* Time to reload the buffer */
  198.     if (out_of_blocks) {
  199.       WARNMS(cinfo->emethods, "Ran out of GIF bits");
  200.       return end_code;        /* fake something useful */
  201.     }
  202.     /* preserve last two bytes of what we have -- assume code_size <= 16 */
  203.     code_buf[0] = code_buf[last_byte-2];
  204.     code_buf[1] = code_buf[last_byte-1];
  205.     /* Load more bytes; set flag if we reach the terminator block */
  206.     if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
  207.       out_of_blocks = TRUE;
  208.       WARNMS(cinfo->emethods, "Ran out of GIF bits");
  209.       return end_code;        /* fake something useful */
  210.     }
  211.     /* Reset counters */
  212.     cur_bit = (cur_bit - last_bit) + 16;
  213.     last_byte = 2 + count;
  214.     last_bit = last_byte * 8;
  215.   }
  216.  
  217.   /* Form up next 24 bits in accum */
  218.   offs = cur_bit >> 3;        /* byte containing cur_bit */
  219. #ifdef CHAR_IS_UNSIGNED
  220.   accum = code_buf[offs+2];
  221.   accum <<= 8;
  222.   accum |= code_buf[offs+1];
  223.   accum <<= 8;
  224.   accum |= code_buf[offs];
  225. #else
  226.   accum = code_buf[offs+2] & 0xFF;
  227.   accum <<= 8;
  228.   accum |= code_buf[offs+1] & 0xFF;
  229.   accum <<= 8;
  230.   accum |= code_buf[offs] & 0xFF;
  231. #endif
  232.